--[[
	Helper for working with the per item lines of scans.
	
	Copyright (C) Udorn (Blackhand)
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.	
--]]

vendor.ScanResults = {};

--[[
	Packs the given entries in a compressed format.
	@return The compressed representation of the parameters.
--]]
function vendor.ScanResults.Pack(itemLink, time, timeLeft, count, minBid, minIncrement, buyoutPrice, bidAmount, owner)
	local keyPart = strjoin(";", itemLink, tostring(count), tostring(minBid), tostring(minIncrement), tostring(buyoutPrice), tostring(bidAmount), owner);
	local timePart = strjoin(";", tostring(time), tostring(timeLeft));
	return keyPart.."#"..timePart;
end
	
--[[
	Unpacks the compressed data into the corresponding parameters.
	@Return key, time, timeLeft, count, minBid, minIncrement, buyoutPrice, bidAmount, owner
--]]
function vendor.ScanResults.Unpack(data)
	local keyPart, timePart = strsplit("#", data)
	local itemLink, count, minBid, minIncrement, buyoutPrice, bidAmount, owner = strsplit(";", keyPart);
	local time, timeLeft = strsplit(";", timePart);
	local key = vendor.Items:GetItemLinkKey(itemLink);
	return key, tonumber(time), tonumber(timeLeft), tonumber(count), tonumber(minBid), tonumber(minIncrement), tonumber(buyoutPrice), tonumber(bidAmount), owner;
end

--[[
	Returns the key from the given compressed data line.
--]]
function vendor.ScanResults.ExtractKey(data)
	local _, _, itemString = string.find(data, "^|c%x+|H(.+)|h%[.*%]");
	local _, itemId, enchantId, jewelId1, jewelId2, jewelId3, jewelId4, suffixId, uniqueId = strsplit(":", itemString)
	return strjoin(":", itemId, jewelId1, jewelId2, jewelId3, jewelId4, suffixId);
end	

function vendor.ScanResults.ExtractItemLink(data)
	local itemLink = strsplit(";", data);
	return itemLink;
end